package top.chmin.project.cloud.builder;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumn;
import top.chmin.project.cloud.common.entity.column.auto.EntityColumnDao;
import top.chmin.project.cloud.common.entity.label.value.auto.EntityLabelValueDao;

import java.util.stream.Collectors;

/**
 * chmin
 * 2023/12/15 15:21
 * cloud-builder
 */
@Component
public class SelectPageBuilder implements Builder {

    @Resource
    EntityColumnDao entityColumnDao;
    @Resource
    EntityLabelValueDao entityLabelValueDao;

    @Override
    public void build(BuilderConfig config) {
        String content = """
                package top.chmin.project.cloud.#e.module#.#e.pack#.auto;
                
                #import#
                                
                /**
                 * chmin
                 * #dateTime#
                 * cloud-builder
                 */
                public interface #e.simpleName#SelectPage {
                                
                    @PostMapping("/au/#e.fieldName#/selectPage")
                    Page<Result> selectPage(@RequestBody Param param);
                
                    @Getter
                    @Setter
                    @Accessors(chain = true)
                    class Param extends Page<Result> {
                %s
                %s
                    }
                
                    @Getter
                    @Setter
                    @Accessors(chain = true)
                    class Result {
                %s
                // field relation auto
                    }
                
                }
                """.formatted(
                config.getEntityColumnList().stream()
                        .filter(f -> !"LocalDateTime".equals(f.getSimpleType()))
                        .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType# #f.fieldName#;"))
                        .collect(Collectors.joining("\n")),
                config.getEntityColumnList().stream()
                        .filter(f -> "LocalDateTime".equals(f.getSimpleType()))
                        .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType#[] #f.fieldName#;"))
                        .collect(Collectors.joining("\n")),
                config.getEntityColumnList().stream()
                        .map(f -> fixEntityColumn(f, "        // #f.remark#\n        #f.simpleType# #f.fieldName#;"))
                        .collect(Collectors.joining("\n"))
//                config.getEntityColumnList().stream()
//                        .filter(f -> f.getLabelId() != null)
//                        .map(f -> {
//                            FieldInfo labelEntityColumn = getFieldInfo(fieldInfoDao, f.getLabelId());
//                            return fixEntityColumn(labelEntityColumn, "\n        // #f.remark#\n        #f.simpleType# %s#FN#;".formatted(f.getFieldName()));
//                        })
//                        .collect(Collectors.joining())
        );

        save(config, config.getEntity().getClassName() + "SelectPage", content);

        config.getEntityColumnRelationList().forEach(r -> {
            EntityColumn column = config.getEntityColumnList().stream().filter(f -> f.getFieldCode().equals(r.getSourceFieldCode())).findFirst().orElseThrow();

            EntityColumn targetValue = getEntityColumn(entityColumnDao, r.getTargetFieldCode());

            appendJava(
                    config,
                    column,
                    "// field relation auto",
                    "String %sLabel;".formatted(column.getFieldName()),
                    "\n        // %s\n        String %sLabel;".formatted(targetValue.getRemark(), column.getFieldName()),
                    config.getEntity().getClassName() + "SelectPage"
            );
        });
    }
}
